{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get game logs for each team "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.8.0.1'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import goldsberry\n",
    "import os\n",
    "import pandas as pd\n",
    "import csv\n",
    "from goldsberry.team import *\n",
    "pd.set_option(\"display.max_columns\",150) # Change Pandas Display Options\n",
    "goldsberry.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def ensure_dir(f):\n",
    "    d = os.path.dirname(f)\n",
    "    if not os.path.exists(d):\n",
    "        os.makedirs(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def write_csv(team_stats, season):\n",
    "    if (len(team_stats.logs()) < 1):\n",
    "        return\n",
    "    team_id = team_stats.logs()[0]['Team_ID']\n",
    "    folder_name =  os.path.join(os.getcwd(), season + '_team_stats')    \n",
    "    complete_name = os.path.join(folder_name, str(season) + '_' + str(team_id) + '.csv')\n",
    "    print complete_name\n",
    "    ensure_dir(complete_name)    \n",
    "    with open(complete_name, 'w') as csvfile:\n",
    "        fieldnames = ['AST','BLK','DREB','FG3A','FG3M','FG3_PCT','FGA','FGM','FG_PCT','FTA',\n",
    "                      'FTM','FT_PCT','GAME_DATE','Game_ID','MATCHUP','MIN','OREB','PF',\n",
    "                      'PTS','REB','STL','TOV','Team_ID','WL']\n",
    "        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)\n",
    "\n",
    "        writer.writeheader()\n",
    "        for team_row in team_stats.logs():\n",
    "            writer.writerow({\n",
    "                          'AST': team_row['AST'],\n",
    "                          'BLK': team_row['BLK'],\n",
    "                          'DREB': team_row['DREB'],\n",
    "                          'FG3A': team_row['FG3A'],\n",
    "                          'FG3M': team_row['FG3M'],\n",
    "                          'FG3_PCT': team_row['FG3_PCT'],\n",
    "                          'FGA': team_row['FGA'],\n",
    "                          'FGM': team_row['FGM'],\n",
    "                          'FG_PCT': team_row['FG_PCT'],\n",
    "                          'FTA': team_row['FTA'],\n",
    "                          'FTM': team_row['FTM'],\n",
    "                          'FT_PCT': team_row['FT_PCT'],\n",
    "                          'GAME_DATE': team_row['GAME_DATE'],\n",
    "                          'Game_ID': team_row['Game_ID'],\n",
    "                          'MATCHUP': team_row['MATCHUP'],\n",
    "                          'MIN': team_row['MIN'],\n",
    "                          'OREB': team_row['OREB'],\n",
    "                          'PF': team_row['PF'],\n",
    "                          'PTS': team_row['PTS'],\n",
    "                          'REB': team_row['REB'],\n",
    "                          'STL': team_row['STL'],\n",
    "                          'TOV': team_row['TOV'],\n",
    "                          'Team_ID': team_row['Team_ID'],\n",
    "                          'WL': team_row['WL']})\n",
    "        print \"Done.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "## get all team ids\n",
    "gameids = pd.DataFrame(goldsberry.GameIDs().game_list())\n",
    "filter_season = '22015'\n",
    "teamids_2015 = gameids['TEAM_ID'].ix[gameids['SEASON_ID']==filter_season].drop_duplicates().tolist()\n",
    "\n",
    "## for each team, get the game logs and write in a csv -- in the given folder\n",
    "for team in teamids_2015:    \n",
    "    team_stats = goldsberry.team._Team2.game_logs(team)\n",
    "    write_csv(team_stats, filter_season)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
